﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using BLL;
using NLog;

namespace QLTTTH
{
    public partial class frmThongTinLopHoc : DevExpress.XtraEditors.XtraForm
    {
        private Logger logger = LogManager.GetCurrentClassLogger();

        public frmThongTinLopHoc()
        {
            InitializeComponent();
        }

        public void FillControl(LopHoc lh)
        {
            this.txtMaLop.Text = lh.MaLop.ToString();
            this.loupGiaoVien.EditValue = lh.MaGV;
            this.loupKhoaHoc.EditValue = lh.MaKH;
            this.txtTenLop.Text = lh.TenLop;
            this.txtSiSo.Text = lh.SiSo.ToString();

        }

        private void frmThongTinLopHoc_Load(object sender, EventArgs e)
        {
            var aResult = KhoaHocManager.LayDanhSachKhoaHoc();
            if (aResult.Success)
            {
                loupKhoaHoc.Properties.DataSource = aResult.Result;
                loupKhoaHoc.Properties.ValueMember = "MaKH";
                loupKhoaHoc.Properties.DisplayMember = "TenKH";
            }
            if (loupKhoaHoc.ItemIndex < 0)
                loupKhoaHoc.ItemIndex = 0;

            var bResult = GiaoVienManager.LoadGiaoVien_List();
            if (bResult.Success)
            {
                loupGiaoVien.Properties.DataSource = bResult.Result;
                loupGiaoVien.Properties.ValueMember = "MaNV";
                loupGiaoVien.Properties.DisplayMember = "HoTen";
            }
            if (loupGiaoVien.ItemIndex < 0)
                loupGiaoVien.ItemIndex = 0;
            

        }

        public delegate void SendData(LopHoc lh);

        public SendData sendData;

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (sendData != null)
            {
                var vResutl = new LopHocManager().Validate(GetLopHoc());
                if (vResutl.Valid)
                {
                    sendData(GetLopHoc());
                    DialogResult = DialogResult.OK;
                    this.Close();
                }
                else
                {
                    logger.Info(vResutl.ToErrorString());
                    XtraMessageBox.Show(vResutl.ToString(), "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private LopHoc GetLopHoc()
        {
            var lh = new LopHoc();
            if (!txtMaLop.Text.ToString().Equals(""))
            {
                lh.MaLop = int.Parse(txtMaLop.Text.ToString());
            }
            lh.MaGV = int.Parse(loupGiaoVien.EditValue.ToString());
            lh.MaKH = int.Parse(loupKhoaHoc.EditValue.ToString());
            lh.TenLop = txtTenLop.Text.ToString();
            if (!txtSiSo.Text.Equals(""))
            {
                lh.SiSo = int.Parse(txtSiSo.Text);
            }
            else
            {
                lh.SiSo = -1;
            }

            return lh;
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}